package com.loong.shop.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.common.rabbitmq.dto.GoodsBaseDTO;
import com.loong.common.rabbitmq.dto.base.RepositoryEventDto;
import com.loong.common.utils.TransformUtils;
import com.loong.shop.common.backend.param.GoodsBaseQueryParam;
import com.loong.shop.model.GoodsBase;
import com.loong.shop.mq.GoodsReport;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: 刘群
 * @Date: 2020/10/27 7:46
 */
@Slf4j
@Repository
public class GoodsBaseBusiness extends AbstractBusiness<GoodsBase> {

    @Resource
    GoodsReport goodsReport;

    /**
     * 根据查询参数查询商品
     * @param param 查询参数：品牌id、商品分类id、商品名称、店铺id
     * @return 商品集合
     */
    public List<GoodsBase> findGoodsBaseByParam(GoodsBaseQueryParam param){
        Condition condition = new Condition(GoodsBase.class);
        Example.Criteria criteria = condition.createCriteria();
        if (ObjectUtils.isNotEmpty(param)){
            if (ObjectUtils.isNotEmpty(param.getBrandId())){
                criteria.andEqualTo("brandId",param.getBrandId());
            }
            if (ObjectUtils.isNotEmpty(param.getCategoryId())){
                criteria.andEqualTo("categoryId",param.getCategoryId());
            }
            if (ObjectUtils.isNotEmpty(param.getGoodsName())){
                criteria.andLike("name","%"+param.getGoodsName()+"%");
            }
            if (ObjectUtils.isNotEmpty(param.getShopId())){
                criteria.andEqualTo("shopId",param.getShopId());
            }
            if (ObjectUtils.isNotEmpty(param.getRecommend())){
                criteria.andEqualTo("recommend",param.getRecommend());
            }
            return findByCondition(condition);
        }
        return null;
    }

    @Override
    public void save(GoodsBase model) {
        //新增商品信息
        super.save(model);
        //上报到MQ中
        GoodsBaseDTO goodsBaseDTO = TransformUtils.transform(model, GoodsBaseDTO.class);
        RepositoryEventDto<GoodsBaseDTO> eventDto = RepositoryEventDto.buildWithCreate(goodsBaseDTO);
        goodsReport.report(eventDto);
    }

    @Override
    public void updateByPrimaryKeySelective(GoodsBase model) {
        //更新商品信息到数据库
        super.updateByPrimaryKeySelective(model);
        //上报到MQ中
        GoodsBaseDTO goodsBaseDTO = TransformUtils.transform(model, GoodsBaseDTO.class);
        RepositoryEventDto<GoodsBaseDTO> eventDto = RepositoryEventDto.buildWithUpdate(goodsBaseDTO);
        goodsReport.report(eventDto);
    }

    @Override
    public void deleteById(Object id) {
        //删除数据库中的商品信息
        super.deleteById(id);
        //上报到MQ中
        GoodsBaseDTO goodsBaseDTO = GoodsBaseDTO.builder().id((Long)id).build();
        RepositoryEventDto<GoodsBaseDTO> eventDto = RepositoryEventDto.buildWithDelete(goodsBaseDTO);
        goodsReport.report(eventDto);
    }
}
